<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增终端撤机记录')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-record-add" enctype="multipart/form-data">
            <div class="form-group">
                <label class="col-sm-3 control-label  is-required">服务商编号：</label>
                <div class="col-sm-8">
                    <input name="agentTarNum" onclick="selectAgent()" readonly id="xagentNum" type="text" class="form-control" required>
                    <i class="fa fa-search" style="margin-top: -22px;margin-right:3px;float:right;"></i>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">服务商名称：</label>
                <div class="col-sm-8">
                    <input name="agentTarName" class="form-control" id="xagentName" type="text" required readonly>
                </div>
            </div>
            <div class="form-group" style="display: none;">
                <label class="col-sm-3 control-label is-required">撤机台数：</label>
                <div class="col-sm-8">
                    <input name="total" id="total" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">机具类型：</label>
                <div class="col-sm-8">
                    <select name="posType" readonly  class="form-control" th:with="type=${@dict.getType('terminal_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                                th:selected = "${dict.dictValue eq agentApplyPos?.posType}" ></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">终端文件：</label>
                <div class="col-sm-8">
                    <span class="btn btn-white btn-file">
                        <input type="file" name="file" id="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
                    </span>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "terminal/record"
        $("#form-record-add").validate({
            focusCleanup: true

        });

        /**
         * 上传文件根据读取文件信息填写还机台数
         */
        $("#file").change(function (e){
            var fileData = new FormData();
            if ($("[name = 'file']")[0].files[0]==undefined){
                return;
            }else{
                fileData.append("file", $("[name = 'file']")[0].files[0]);
                $.ajax({
                    url : prefix + "/file",
                    type: 'POST',
                    data: fileData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success : function(result) {
                        $("#total").val(result.data);
                    },
                    error : function(result) {
                        $.modal.alertError(result.msg);
                    }
                })
            }
        });

        function submitHandler() {
            if ($.validate.form()) {
                var formData = new FormData();
                formData.append("file", $("[name = 'file']")[0].files[0]);
                formData.append("agentTarNum",$("[name='agentTarNum']").val());
                formData.append("agentTarName",$("[name='agentTarName']").val());
                formData.append("total",$("[name='total']").val());
                formData.append("type",$("[name='posType']").val());

                formData.append("actId",$("[name='actId']").val());
                formData.append("endDate",$("[name='endDate']").val());
                // $.operate.save(, formData);
                $.ajax({
                    url : prefix + "/addRevoke",
                    type: 'POST',
                    data: formData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍后...");
                        $.modal.disable();
                    },
                    success : function(result) {
                        if (result.code == web_status.SUCCESS) {
                            var parent = window.parent;
                            if (parent.table.options.type == table_type.bootstrapTable) {
                                $.modal.close();
                                parent.$.modal.msgSuccess(result.msg);
                                parent.$.table.refresh();
                            } else if (parent.table.options.type == table_type.bootstrapTreeTable) {
                                $.modal.close();
                                parent.$.modal.msgSuccess(result.msg);
                                parent.$.treeTable.refresh();
                            } else {
                                $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
                            }
                        } else if (result.code == web_status.WARNING) {
                            $.modal.alertWarning(result.msg)
                        }  else {
                            $.modal.alertError(result.msg);
                        }
                        $.modal.closeLoading();
                        $.modal.enable();
                        //$.modal.alertSuccess(data.msg)
                    },
                    error : function(result) {
                        $.modal.alertError(result.msg);
                    }
                });
            }
        }
        function selectAgent() {
            var url =prefix+ "/selectAgentInfo";
            $.modal.open("选择服务商" , url, '900','500');
        }

        function selectAct() {
            var url =prefix+ "/selectActList";
            $.modal.open("选择活动" , url, '700');
        }

    </script>
</body>
</html>